package cn.tedu.qxhouserent.house.controller;

import cn.tedu.qxhouserent.commons.pojo.vo.PageData;
import cn.tedu.qxhouserent.commons.security.LoginPrincipal;
import cn.tedu.qxhouserent.commons.web.JsonResult;
import cn.tedu.qxhouserent.house.pojo.vo.NewsCategoryListItemVO;
import cn.tedu.qxhouserent.house.pojo.vo.NewsCategoryStandardVO;
import cn.tedu.qxhouserent.house.pojo.vo.NewsListVO;
import cn.tedu.qxhouserent.house.service.NewsCategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news-category")
@Api(tags = "新闻分类管理模块")
@Slf4j
public class NewsCategoryController {
    @Autowired
    NewsCategoryService newsCategoryService;

    @GetMapping("/list")
    @ApiOperation("查询新闻分类列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list() {
        log.debug("开始处理【查询新闻分类列表】的请求，参数: 无");

        List<NewsCategoryListItemVO> list = newsCategoryService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/list/cha")
    @ApiOperation("搜索新闻查询")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "新闻id")
    })
    public JsonResult cha(@Range(min = 1, message = "请输入正确的id") @RequestParam Long id){
        List<NewsListVO> c = newsCategoryService.cha(id);
        return JsonResult.ok(c);
    }

    @GetMapping("/list/shoucang")
    @ApiOperation("根据用户id查询收藏的资讯")
    @ApiOperationSupport(order = 440)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult listByUserId(@AuthenticationPrincipal LoginPrincipal loginPrincipal,
                                   @Range(min = 1, message = "查询用户列表失败，请提供正确的页码值！") Integer page){
        log.debug("开始处理【根据用户id查询房源列表】的请求,用户id: {}, 页码:{}",loginPrincipal.getId(),page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<NewsListVO> pageData = newsCategoryService.listCategoryByUserId(loginPrincipal.getId(), page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/list/mochabyuid")
    @ApiOperation("根据用户id模糊查询收藏的资讯")
    @ApiOperationSupport(order = 450)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult listMoChaByUId(@AuthenticationPrincipal LoginPrincipal loginPrincipal,
                                   @Range(min = 1, message = "查询用户列表失败，请提供正确的页码值！") Integer page,
                                String s){
        log.debug("开始处理【根据用户id查询房源列表】的请求,用户id: {}, 页码:{},咨询内容：{}",loginPrincipal.getId(),page,s);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<NewsListVO> pageData = newsCategoryService.listMoChaByUId(loginPrincipal.getId(), page, s);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/list/mochabyzid")
    @ApiOperation("根据咨询id模糊查询资讯")
    @ApiOperationSupport(order = 460)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "咨询Id" , paramType = "query")
    })
    public JsonResult listMoChaByZId(
                                @Range(min = 1, message = "查询用户列表失败，请提供正确的页码值！") Integer page,
                                @Range(min = -10, message = "请输入正确的id")Integer id,
                                String s){
        log.debug("开始处理【根据用户id查询房源列表】的请求,用户id: {}, 页码:{},咨询id：{},咨询内容：{}",page,id,s);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<NewsListVO> pageData = newsCategoryService.listMoChaByZId(page, id, s);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询新闻分类名称")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "咨询详情ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询新闻分类名称详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询咨询详情详情】的请求，参数：{}", id);
        NewsCategoryStandardVO result = newsCategoryService.getStandardById(id);
        return JsonResult.ok(result);
    }
}
